#include "tabmodel3.h"

TabModel3::TabModel3( QObject * parent,
                      QSqlDatabase db,
                      QMap<int, int> asc_indices_arg,
                      QMap<int, int> desc_indices_arg )
    : QSqlTableModel( parent, db )
{
setup_map();
asc_indices = asc_indices_arg;
desc_indices = desc_indices_arg;
}

TabModel3::~TabModel3()
{
}

Qt::ItemFlags TabModel3::flags( const QModelIndex & index ) const
{
Qt::ItemFlags flags = QSqlTableModel::flags(index);

    //if ( index.row() == 0 )
    //{
    //return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    //}

    if ( index.column() == 0 ||
         index.column() == 1 ||
         index.column() == 2 ||
         index.column() == 5 ||
         index.column() == 6 )
    {
    return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    }
return flags;
}

QVariant TabModel3::data( const QModelIndex & index,
                          int role ) const
{
QVariant value = QSqlTableModel::data( index, role );

        //if ( role == Qt::BackgroundRole && index.row() == 0 )
        //{
        //return qVariantFromValue( QColor(Qt::gray) );
        //}

        if ( role == Qt::DisplayRole && index.column() == 0 )
        {
        int i = value.toInt();
        QString col_name = map.value(i);
        QVariant v = QVariant(col_name);
        return v;
        }

        if ( role == Qt::TextColorRole && index.column() == 0 )
        {
        return qVariantFromValue( QColor(Qt::darkBlue) );
        }

        if ( role == Qt::DisplayRole && index.column() == 1 )
        {
        QVariant v;
        int asc_i = asc_indices.value( index.row(), -1 );
            if ( asc_i == 1 )
            {
            QString asc_index = tr("Yes");
            v = QVariant(asc_index);
            }
        return v;
        }

        if ( role == Qt::DisplayRole && index.column() == 2 )
        {
        QVariant v;
        int desc_i = desc_indices.value( index.row(), -1 );
            if ( desc_i == 1 )
            {
            QString desc_index = tr("Yes");
            v = QVariant(desc_index);
            }
        return v;
        }

        if ( role == Qt::DisplayRole && index.column() == 3 )
        {
        int i = value.toInt();
        QVariant v;
            if ( i == 1)
            {
            QString show = tr("Show");
            v = QVariant(show);
            }
            else
            {
            QString hide = tr("Hide");
            v = QVariant(hide);
            }
        return v;
        }
        
return value;
}

void TabModel3::setup_map()
{
map.clear();
Schema * schema = new Schema;
    if ( schema == NULL )
    {
    return;
    }
map = schema->headers();
}
